<template>
	<view class="content-box">
		<span>{{ title }}</span>
		<text v-if="bySelf">{{ notice }}</text>
		<text v-else-if="notice !== '等待对方处理请求....'">{{ notice }}</text>
		<view v-else class="button-box" :class="bySelf?'ziji':'duifang'">
			<u-button v-if="show" @click="agree(1)" >接受</u-button>
			<u-button v-if="show" @click="agree(2)" >拒绝</u-button>
		</view>
		<ChatSign ref="addressWindow" :isSign='isSign' @changeClose="() => isSign = false" :shipid='shipid'></ChatSign>
	</view>
</template>

<script>
import { signAgreement } from "@/api/my.js";
import { handleShip } from '@/api/chat.js';
import ChatSign from '@/components/chatSign'
export default {
	name: "imcontent",
	components: {
		ChatSign
	},
	props: {
		content: {
			type: String,
			default: ''
		},
		bySelf: {
			type: Boolean,
			default: false
		},
		type: {
			type: String,
			default: ''
		},
		userid: {
			type: Number,
			default: 0
		},
		messageid: {
			type: Number,
			default: 0
		},
		shipid: {
			type: Number,
			default: 0
		},
	},

	watch: {
		sign(newVal, oldVal) {
			if (newVal !== '') {

			} else {
				console.log('签名为空');

			}
		},
	},

	mounted() {
		console.log(this.bySelf, '------------');
		const parts = this.content.split(',');
		// 获取拆分后的结果
		this.title = parts[0]; // 逗号前的部分
		this.notice = parts[1]; // 逗号后的部分
	},
	data() {
		return {
			isSign: false,
			shipType: 0, //1是情侣 2是闺蜜
			state: 0,//2解绑 3绑定
			title: '',
			notice: '',
			show: true,
			sign:''
		};
	},
	methods: {
		async agree(agree) {
			console.log(this.signimg, '签名');
			// this.getData();
			let res = await signAgreement({
				'ship_id': this.shipid,
			}).then(res => {
				console.log(res, '是否签署协议');
				this.sign = res.data
				console.log(this.sign, '签名');
				//签名为空的话先签名
				if(agree == 2){
					this.handle(agree)
				}else{
					if (!this.sign) {
						this.isSign = true
						console.log(this.sign, '签名为空');
					} else {
						//不为空直接调用同意接口
						this.handle(agree)
					}
				}
				
			});
			

		},
		rightClick() {
			console.log('保存')
		},
		//处理关系
		async handle(agree) {
			this.hadleType()

			let data = {
				'type': this.shipType,
				'agree': agree,
				'state': this.state,
				'user_id': this.userid,
				'message_id': this.messageid,
				'ship_id': this.shipid
			}
			console.log(data, '请求数据------');

			const res = await handleShip(data).then(res => {
				uni.showToast({
					'icon': 'none',
					'title': res.msg,
					'duration': 1000
				})
				if (agree == 1) {
					this.notice = '已经同意了请求'
					this.show = false
				}
				if (agree == 2) {
					this.notice = '已经拒绝了请求'
					this.show = false
				}

			});
		},
		//处理类型
		hadleType() {
			console.log(this.type, '什么类型');
			if (this.type == "love") {
				this.shipType = 1
				this.state = 3
			}
			//闺蜜邀请
			if (this.type == "bestie") {
				this.shipType = 2
				this.state = 3
			}
			//情侣解绑
			if (this.type == "securelove") {
				this.shipType = 1
				this.state = 2
			}
			//闺蜜解绑
			if (this.type == "securebestie") {
				console.log('闺蜜解绑');
				this.shipType = 2
				this.state = 2
			}
		}
	}
}
</script>

<style lang="scss" scoped>
	.ziji{
		/deep/.u-button {
			color: #FFFFFF;
			background-color: #252525;
			border:none;
		}
	}
	.duifang{
		/deep/.u-button {
			color: #252525;
			background: #F6F6F6;
			border:none;
		}
	}
.content-box {
	width: 100%;
	height: 100px;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: space-evenly;

	.button-box {
		display: flex;
		align-items: center;
		justify-content: space-around;
		width: 100%;

		
	}
}
</style>